<?php
class Sender extends CronCommon
{
	
	var $tbl_pref_custom = 'kb_';
	var $tables = array('entry'=>'entry',
						'category'=>'category',
						'entry_to_category'=>'entry_to_category'
						);
	var $custom_tables = array('user'=>'user', 'data_to_user_rule', 'data_to_user_value');
	
	var $debug = false;
		

	
	function isFreshArticles($last_sent_date) {
		$sql = "SELECT COUNT(*) AS num
		FROM {$this->tbl->entry} e
		WHERE (e.date_posted > '{$last_sent_date}' 
				OR e.date_updated > '{$last_sent_date}')";
		
		//echo "<pre>"; print_r($sql); echo "</pre>";
		$result =& $this->db->Execute($sql) or die(db_error($sql));
		return $result->Fields('num');			
	}

	
	function getArticlesSql($last_sent_date, $body_words = 100) {
		
		$body_words_sql = ($body_words != 'all') 
						  ? "SUBSTRING_INDEX(e.body,' ',$body_words)" 
						  : 'e.body';		
		
		$sql = "
		SELECT 
			e.id,
			e.title,
			{$body_words_sql} AS body,
			cat.id AS category_id,
			cat.name AS category_name,
			UNIX_TIMESTAMP(e.date_posted) AS ts_posted,
			UNIX_TIMESTAMP(e.date_updated) AS ts_updated,
			IF(e.date_updated > e.date_posted, 'updated', 'posted') as type 
		FROM 
			{$this->tbl->entry} e,
			{$this->tbl->category} cat,
			{$this->tbl->entry_to_category} e_to_cat
		WHERE 1
			AND e.id = e_to_cat.entry_id
			AND cat.id = e_to_cat.category_id
			AND (e.date_posted > '{$last_sent_date}' 
		         OR e.date_updated > '{$last_sent_date}')
			AND {$this->sql_params}
			ORDER BY e.date_updated";
		
		//echo "<pre>"; print_r($sql); echo "</pre>";
		return $sql;
	}
	
	
	function &getArticles($last_sent_date, $body_words = 100) {
		$sql = $this->getArticlesSql($last_sent_date);
		$result = &$this->db->Execute($sql) or die(db_error($sql));
		return $result;
	}
	
	
	function getSubscribersSql($category_id) {
		$sql = "
		SELECT 
			u.*
		FROM 
			{$this->tbl->data_to_user_rule} d, 
			{$this->tbl->data_to_user_value} dv,
			{$this->tbl->user} u
		WHERE 1
			AND d.title = 'kb_category_to_admin'
			AND d.id = dv.rule_id
			AND dv.data_value IN ($category_id)
			AND dv.user_value = u.id";
			
		return $sql;
	}
	
	
	function getSubscribers($category_id) {
	
		$sql = $this->getSubscribersSql($category_id);
		$result =& $this->db->Execute($sql) or die(db_error($sql));
		return $result->GetArray();
	}
	
	
	function getLetter($record, $user = false) {
		
		$controller = &new KBClientController;
		$controller->mod_rewrite = false;
		
		$tpl = &new tplTemplatez(APP_EMAI_TMPL_DIR . 'updated_articles.html');
		
		$link = $controller->getLink('entry', $record['category_id'], $record['id']);
		$link = substr($link, strpos($link, '?'));
		$link = APP_CLIENT_PATH . 'index.php' . $link;
		
		$record['article_link'] = $link;
		$record['body'] = $this->getSummary($record['body']);
		$record['date'] = strftime('%d %b, %Y', $record['ts_posted']); 
				
		$tpl->tplParse($record, 'row');

		$tpl->tplParse();
		return $tpl->tplPrint(1);
	}
	
	
	function send($letter, $data) {
		
		$mail = new PHPMailer();
		$mail->From 	= 'noreplay@';
		$mail->FromName = 'KB Mailer';
		$mail->Sender 	= 'KB Mailer';
		$mail->Subject 	= 'New or Updated articles';
		$mail->IsHTML(true);
	
		$mail->AddAddress($data['email']);
		$mail->Body =& $letter;

		echo "<pre>"; print_r($mail); echo "</pre>";
		//exit;
		
		//$sent = $mail->Send();
			
		return $sent;
	}
	
	
/*
	// return last date sent or false if no time to send
	function isTimeToSend() {
		$sql = "SELECT sent_date FROM {$this->tbl->letter_time} 
		WHERE 1
		AND FIND_IN_SET(DAYOFWEEK(CURRENT_DATE)-1, send_day)
		AND send_time <= CURRENT_TIME
		AND sent_date < CURRENT_DATE
		AND lang = '{$this->content_lang}'";
		
		$result =& $this->db->Execute($sql) or die(db_error($sql));
		$sent_date = $result->Fields('sent_date');
		return ($sent_date) ? $sent_date : false;
	}
*/


/*
	function getArticlesRecords() {
		
		$data = array();
		$data['posted'] = array();
		$data['updated'] = array();
		
		$this->seSqlParams("AND (e.date_posted > '{$this->last_sent_date}' 
		                    OR e.date_updated > '{$this->last_sent_date}')");
		$this->setSqlParamsOrder('e.date_updated');
							
		$sql = $this->getArticlesSql();
		$result =& $this->db->Execute($sql) or die(db_error($sql));
		while($row = $result->FetchRow()) {
			
			$data[$row['type']][][$row['id']] = $row;
		}
		
		return $data;
	}
*/




/*
	$pause  = ($i % SENDER_MAIL_ONCE) ? false : true;
	if($pause) { 
		sleep(SENDER_SLEEP_TIME);
	}
	$i++; 
*/
}
?>